// 完成异步
// class MyPromise {
//   constructor(executor) {
//     this.state = 'pending'
//     this.value = ''
//     this.reason = ''
//     this.onResolvedCallback = []
//     this.onRejectedCallback = []
//     let resolve = (value) => {
//       if (this.state === 'pending') {
//         this.state = 'fullFilled'  //改变状态
//         this.value = value   //存入形参的值
//         this.onResolvedCallback.forEach(fn => {
//           fn()
//         })
//       }
//     }
//     let reject = (reason) => {
//       if (this.state === 'pending') {
//         this.state = 'rejected'
//         this.reason = reason
//         this.onRejectedCallback.forEach(fn => fn())
//       }
//     }
//     try {
//       executor(resolve, reject)
//     }
//     catch {
//       reject(reason)
//     }
//   }
//   then(onFullFilled, onRejected) {
//     if (this.state === 'fullFilled') {
//       onFullFilled(this.value)
//     }
//     if (this.state === 'rejected') {
//       onRejected(this.reason)
//     }
//     if (this.state === 'pending') {
//       this.onResolvedCallback.push(() => {
//         onFullFilled(this.value)
//       })
//       this.onRejectedCallback.push(() => {
//         onRejected(this.reason)
//       })
//     }
//   }
// }

/**
 * 链式调用说明then方法返回的是Promise对象
 * * 每次你对Promise调用then，它都会创建并返回一个新的Promise
 * * return new Promise(resolve,reject) => {....}
 * * 因为是箭头函数,所以第一次调用的this是第一次的调用then方法的this
 * * 即第一次实例的this.所以重写then方法,让它返回一个新的promise对象
 * 
 */

// class MyPromise {
//   constructor(executor) {
//     this.state = 'pending'
//     this.value = ''
//     this.reason = ''
//     this.onResolvedCallback = []
//     this.onRejectedCallback = []
//     let resolve = (value) => {
//       if (this.state === 'pending') {
//         this.state = 'fullFilled'  //改变状态
//         this.value = value   //存入形参的值
//         this.onResolvedCallback.forEach(fn => {
//           fn()
//         })
//       }
//     }
//     let reject = (reason) => {
//       if (this.state === 'pending') {
//         this.state = 'rejected'
//         this.reason = reason
//         this.onRejectedCallback.forEach(fn => fn())
//       }
//     }
//     try {
//       executor(resolve, reject)
//     }
//     catch {
//       reject(reason)
//     }
//   }
//   then(onFullFilled, onRejected) {
//     return new MyPromise((resolve, reject) => {
//       if (this.state === 'fullFilled') {
//         onFullFilled(this.value)
//       }
//       if (this.state === 'rejected') {
//         onRejected(this.reason)
//       }
//       if (this.state === 'pending') {
//         this.onResolvedCallback.push(() => {
//           onFullFilled(this.value)
//         })
//         this.onRejectedCallback.push(() => {
//           onRejected(this.reason)
//         })
//       }
//     })
//   }
// }

/**
 * 返回一个新的promise对象后需要怎么做？
 * * 获取到上一个promise的返回的成功的值/失败的error
 * * 递归, 将获取到的值通过resolve或者reject的方式再次丢出去
 * * 因为可能存在上个promise对象返回的error,所以依旧需要用try,catch去捕获
 */
class MyPromise {
  constructor(executor) {
    this.state = 'pending'
    this.value = ''
    this.reason = ''
    this.onResolvedCallback = []
    this.onRejectedCallback = []
    let resolve = (value) => {
      if (this.state === 'pending') {
        this.state = 'fullFilled'  //改变状态
        this.value = value   //存入形参的值
        this.onResolvedCallback.forEach(fn => {
          fn()
        })
      }
    }
    let reject = (reason) => {
      if (this.state === 'pending') {
        this.state = 'rejected'
        this.reason = reason
        this.onRejectedCallback.forEach(fn => fn())
      }
    }
    try {
      executor(resolve, reject)
    }
    catch {
      reject(reason)
    }
  }
  then(onFullFilled, onRejected) {
    return new MyPromise((resolve, reject) => {
      let x
      if (this.state === 'fullFilled') {
        try {
          x = onFullFilled(this.value)  //链式调用的时候会返回一个值
          resolve(x)
        } catch (e) {
          reject(e)
        }
      }

      if (this.state === 'rejected') {
        try {
          x = onRejected(this.reason)  //链式调用的时候会返回一个值
          resolve(x)
        } catch (e) {
          reject(e)
        }
        // onRejected(this.reason)
      }
      if (this.state === 'pending') {
        this.onResolvedCallback.push(() => {
          try {
            x = onFullFilled(this.value)  //链式调用的时候会返回一个值
            resolve(x)
          } catch (e) {
            reject(e)
          }
        })
        this.onRejectedCallback.push(() => {
          try {
            x = onRejected(this.reason)  //链式调用的时候会返回一个值
            resolve(x)
          } catch (e) {
            reject(e)
          }
        })
      }
    })
  }
}



var p = new MyPromise((resolve, reject) => {
  // setTimeout(() =>{
  //   resolve(10)
  // })
})


p.then(res => { console.log("success", res); return res + 2 },
  reason => { console.log("fail", reason); return reason })
  .then(res => { console.log("success", res); return res + 2 }, reason => { console.log("fail", reason); return reason })
  .then(res => { console.log("success", res); return res }, reason => { console.log("fail", reason); return reason })